<template>
    <div class="Jcommon-layout">
        <div class="Jcommon-layout-center">
            <el-row class="Jcommon-search-box" :gutter="16">
                <el-form :model="queryParams" ref="queryForm" v-show="showSearch">
                    <el-col :span="6">
                        <el-form-item prop="parkingCode">
                            <el-input v-model="queryParams.parkingCode" placeholder="请选择停车场" clearable size="small">
                                <template slot="append">
                                    <el-button @click="handleSelectParkOpen" type="primary" icon="el-icon-search">选择</el-button>
                                </template>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="车位编号" prop="carCode">
                            <el-input v-model="queryParams.carCode" placeholder="请输入车位编号" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="车位状态" prop="status">
                            <el-select v-model="queryParams.status" placeholder="请选择车位状态">
                                <el-option v-for="dict in dict.type.park_space_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>
            <div class="Jcommon-layout-main Jflex-main">
                <div class="Jcommon-head">
                    <el-row :gutter="10" class="mb8">
                        <el-col :span="1.5">
                            <el-button type="primary" icon="el-icon-plus" @click="handleAdd" v-hasPermi="['pms:building:add']">新增</el-button>
                        </el-col>
                    </el-row>
                    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
                </div>

                <JTable v-loading="loading" :data="parkingSpaceList">
                    <el-table-column label="停车场" align="center" prop="parkingCode" />
                    <el-table-column label="车位" align="center" prop="carCode" />
                    <el-table-column label="车位状态" align="center" prop="status">
                        <template slot-scope="scope">
                            <dict-tag :options="dict.type.park_space_status" :value="scope.row.status" />
                        </template>
                    </el-table-column>
                    <el-table-column label="车位类型" align="center" prop="type">
                        <template slot-scope="scope">
                            <dict-tag :options="dict.type.park_space_type" :value="scope.row.type" />
                        </template>
                    </el-table-column>
                    <el-table-column label="面积" align="center" prop="area" />
                    <el-table-column label="创建时间" align="center" prop="createDate" />
                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['pms:building:edit']">修改</el-button>
                            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['pms:building:remove']">删除</el-button>
                        </template>
                    </el-table-column>
                </JTable>

                <pagination v-show="total > 0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />
            </div>
        </div>

        <!-- 添加或修改车位管理对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="车位编码" prop="carCode">
                    <el-input v-model="form.carCode" placeholder="请输入车位编码" />
                </el-form-item>
                <el-form-item label="停车场" prop="parkingId">
                    <el-select v-model="form.parkingId" placeholder="请选择停车场">
                        <el-option v-for="item in parkingList" :key="item.id" :label="item.parkingCode" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="车位类型" prop="type">
                    <el-select v-model="form.type" placeholder="请选择车位类型">
                        <el-option v-for="dict in dict.type.park_space_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="面积" prop="area">
                    <el-input v-model="form.area" placeholder="请填写面积" />
                </el-form-item>
                <el-form-item label="备注" prop="remarks">
                    <el-input v-model="form.remarks" placeholder="请输入备注" type="textarea" :rows="3" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <!--选择停车场-->
        <el-dialog title="选择停车场" :visible.sync="selectVisible" width="800px" append-to-body :close-on-click-modal="false">
            <el-form ref="elForm" :model="selectForm" label-width="100px" inline>
                <el-form-item label="停车场编号" prop="parkingCode">
                    <el-input v-model="selectForm.parkingCode" placeholder="请输入停车场编号" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuerySelectPark">搜索</el-button>
                </el-form-item>
            </el-form>
            <JTable :data="parkingList" stripe>
                <el-table-column label="停车场ID" align="center" prop="id" />
                <el-table-column label="停车场编号" align="center" prop="parkingCode" />
                <el-table-column label="停车场类型" align="center" prop="parkingType">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.park_place_type" :value="scope.row.parkingType" />
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" @click="handleSelectPark(scope.row)">选择</el-button>
                    </template>
                </el-table-column>
            </JTable>
            <pagination v-show="selectTotal > 0" :total="selectTotal" />
        </el-dialog>
    </div>
</template>

<script>
import { getParkdingSeatDel, getParkdingSeatList, updateParkingSeat, addParkingSeat, delParkingSeat } from '@/api/parking/parkingSeat.js';
import { listParking } from '@/api/parking/areaManage.js';

export default {
    dicts: ['park_space_status', 'park_space_type', 'park_place_type'],
    data() {
        return {
            selectVisible: false, // 停车场弹框
            selectForm: {
                parkingCode: '',
            },
            selectTotal: 0,
            // 遮罩层
            loading: true,
            // 导出遮罩层
            exportLoading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 停车场列表数据
            parkingList: [],
            // 车位表格数据
            parkingSpaceList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                page: 1,
                limit: 10,
                parkingCode: '', // 停车场ID
                carCode: '',
                status: '',
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                carCode: [{ required: true, message: '车位编号不能为空', trigger: 'blur' }],
                parkingId: [{ required: true, message: '请选择停车场', trigger: 'blur' }],
                type: [{ required: true, message: '车位类型不能为空', trigger: 'blur' }],
                area: [{ required: true, message: '面积不能为空', trigger: 'blur' }],
            },
        };
    },
    created() {
        this.getParkingList();
        this.getList();
    },
    methods: {
        // 请选择 停车场
        handleSelectParkOpen() {
            this.selectVisible = true;
            this.selectForm.parkingCode = '';
        },
        // 停车场-搜索
        handleQuerySelectPark() {
            this.getParkingList();
        },
        // 选择了一个停车场
        handleSelectPark(row) {
            this.queryParams.parkingCode = row.parkingCode;
            this.queryParams.parkingId = row.id;
            this.selectVisible = false;
        },
        /** 停车场管理列表 */
        getParkingList() {
            this.loading = true;
            listParking(this.selectForm).then(response => {
                this.parkingList = response.data.list;
                this.selectTotal = response.data.total;
                this.loading = false;
            });
        },
        /** 查询车位管理列表 */
        getList() {
            this.loading = true;
            getParkdingSeatList(this.queryParams).then(response => {
                this.parkingSpaceList = response.data.list;
                this.total = response.data.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                parkingCode: null,
                carCode: null,
                status: null,
                parkingId: '',
            };
            this.resetForm('form');
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.page = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.queryParams.parkingId = '';
            this.handleQuery();
        },
        // 多选框选中数据
        // handleSelectionChange(selection) {
        //     this.ids = selection.map(item => item.id);
        //     this.single = selection.length !== 1;
        //     this.multiple = !selection.length;
        // },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = '添加信息';
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getParkdingSeatDel(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = '修改信息';
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.id) {
                        updateParkingSeat(this.form).then(response => {
                            this.$modal.msgSuccess('修改成功');
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addParkingSeat(this.form).then(response => {
                            this.$modal.msgSuccess('新增成功');
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm('是否确认删除楼栋管理编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delParkingSeat(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess('删除成功');
                })
                .catch(() => {});
        },
        /** 导出按钮操作 */
        // handleExport() {
        //     const queryParams = this.queryParams;
        //     this.$modal
        //         .confirm('是否确认导出所有楼栋管理数据项？')
        //         .then(() => {
        //             this.exportLoading = true;
        //             return exportParking(queryParams);
        //         })
        //         .then(response => {
        //             this.$download.name(response.msg);
        //             this.exportLoading = false;
        //         })
        //         .catch(() => {});
        // },
    },
};
</script>
